package com.maigao.util;

import com.maigao.entity.enums.Week;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.tuple.Pair;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-26 23:32
 **/
@UtilityClass
public class WeekUtils {
    public Pair<LocalDateTime, LocalDateTime> getWeekRange(Week week) {
        LocalDate date;
        switch (week) {
            case PRE:
                date = LocalDate.now().minusDays(7);
                break;
            case NEXT:
                date = LocalDate.now().plusDays(7);
                break;
            case CURRENT:
            default:
                date = LocalDate.now();
                break;
        }
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        LocalDate startDate;
        LocalDate endDate;
        if (dayOfWeek == DayOfWeek.SUNDAY) {
            startDate = date.minusDays(6); // 如果是周日，向前推6天
        } else {
            startDate = date.minusDays(dayOfWeek.getValue() - 1); // 向前推dayOfWeek-1天
        }
        endDate = startDate.plusDays(6);
        return Pair.of(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX));

    }

    public static void main(String[] args) {
        Pair<LocalDateTime, LocalDateTime> weekRange = getWeekRange(Week.CURRENT);
        System.out.println(weekRange);
    }
}
